<#assign module="marketing"/>

<@override name="header">
	<link rel="stylesheet" href="${ctx}/assets/css/bootstrap-multiselect.css">
	<link rel="stylesheet" href="${ctx}/assets/css/datetimepicker.css">
</@override>
<@override name="body">

	<div class="row">
		<div class="col-md-12">
			<div class="panel panel-default">
				<div class="panel-heading">
					添加优惠劵规则
					<label class="control-label"><font color="red">（带*表示必填项）</font></label>
			    </div>
			    <div class="panel-body">
    				<form id="submit_form" class="form-horizontal" method="post">
		               
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>简介：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="title" name="title" placeholder="40字以内 " type="text" required/>
		                    </div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>编码：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="code" name="code" type="text" placeholder="编码为1-4位的大写字母" maxlength="4" required/>
		                    </div>
		                </div>
		                
		                 
		                
		                <div class="form-group" id="type">
		                    <label for="gender" class="col-sm-3 control-label"><em>*</em>类型：</label>
		                    <div class="col-sm-9">
			                    <div class="checkbox" id="type">
			                    	<label>
									  <input type="radio" name="type" value="0" /><em>&nbsp;&nbsp;</em>打折
									</label>
									<label>
									  <input type="radio"  name="type" value="1" /><em>&nbsp;&nbsp;</em>抵扣（￥）
									</label>
								</div>
							</div>
		                </div>
		                
		                
		                
		                <div class="form-group" id="discount_div" style="display:none;">
		                	<label class="col-sm-3 control-label"><em>*</em>折扣：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="discount" name="discount" data-toggle="tooltip"  title="请在此输入大于0小于10的数字" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')" type="text"/>
		                    </div>
		                    <div class="col-sm-3">
		                    	<span class="help-block" id="discount_span"></span>
		                    </div>
		                </div>
		                
		                <div class="form-group" id="money_div" style="display:none;">
		                    <label class="col-sm-3 control-label"><em>*</em>金额：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="money" name="money" type="text"  onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')"  />
		                    </div>
		                    <div class="col-sm-3">
		                    	<span class="help-block" id="money_span"></span>
		                    </div>
		                </div>
		                
		                
		                <div class="form-group">
					  		<label class="col-sm-3 control-label"><em>*</em>服务类型：</label>
					  		<div class="col-sm-3">
			                    <select class="form-control" id="couponType" name="couponType" required >
			                    	<option value="none" >--请选择--</option>
									<option value="all">全选</option>
						  			<option value="zhuanche" >专车</option>
						  			<option value="zhuanxian" >快车</option>
						  		</select>
					  		</div>
		                </div>
		                
		                <div class="form-group" id="errandTypeDiv" style="display:none;">
					  		<label class="col-sm-3 control-label"><em>*</em>跑腿子类型：</label>
					  		<div class="col-sm-3">
		                    <select class="form-control" id="couponTypeId" name="couponTypeId">
					  			<#if errandTypeList??>
						  			<#list errandTypeList as errandType>
						  			<option value="${errandType.id}" >${errandType.typeName?if_exists}</option>
						  			</#list>
						  		</#if>
					  		</select>
					  		</div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>数量：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="number" name="number" placeholder="即最多发放优惠券的数量" type="text"  onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" required/>
		                    </div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>每人最多领取次数：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="eachLimit" name="eachLimit" placeholder="0为不限定" type="text" max="10" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')" required/>
		                    </div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>有效期：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="startTime" name="startTime" type="text" required/>
		                    	<span class="help-block">优惠券的开始时间，由该日期凌晨开始</span>
		                    </div>
		                    <label style="float:left;" class="control-label">-</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="endTime" name="endTime" type="text" />
		                    	<span class="help-block">优惠券的结束时间，由该日期凌晨结束，不填写则不限制结束时间</span>
		                    </div>
		                </div>
		                
		                <div class="form-group" style="margin-left:10px;">
			                <label class="col-sm-3 control-label"></label>
			                <input id="submit_button" type="submit" class="btn btn-primary" value="发布">
			                <input type="button" class="btn btn-default" onclick="window.location.href = '${ctx}/couponrule/list';" value="返回">
			            </div>
					</form>
        		</div>
			</div>
		</div>
	</div>

</@override>
<@override name="footer">
	<script src="${ctx}/assets/js/bootstrap-multiselect.js"></script>
	<script src="${ctx}/assets/js/bootstrap-datetimepicker.min.js"></script>
	<script src="${ctx}/assets/js/bootstrap-datetimepicker.zh-CN.js"></script>
<script type="text/javascript">

	$(document).ready(function(){
		$("#startTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true, startDate: new Date()});
		$("#endTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true, startDate: new Date()});
		
		$("#discount").tooltip($("[data-toggle='tooltip']").tooltip());
		if($("#couponType").val() == "paotui"){
			$("#errandTypeDiv").css("display","block");
		}
		$("input[name=type]").change(function(){
			if($(this).val()==1){
				$("#discount_div").hide();
				$("#money_div").slideDown();
				
			}
			else if($(this).val()==0){
				$("#money_div").hide();
				$("#discount_div").slideDown();
				
			}

		});
		
		$("#couponType").change(function(){
			if ("paotui"!=$(this).val()) {
				$("#errandTypeDiv").css("display","none");
			} else {
				$("#errandTypeDiv").css("display","block");
			}
		});
		
		
		$("#submit_form").submit(function(){
		
			
			var code=$("#code").val();
			var isE=/^([A-Z]{1,4})$/;
			if(!isE.test(code)){
		       $.scojs_message('无效的编码，编码为1-4位的大写字母', $.scojs_message.TYPE_ERROR);
		       $("#submit_button").attr("disabled",false);
		       return false;
		    }
		    
	    	var title = $('#title').val();
	    	var type =  $('input[name=type]:checked').val();
	    	var couponType = $('#couponType').val();
	    	var couponTypeId = $('#couponTypeId').val();
	    	var number = $('#number').val();
	    	var eachLimit = $('#eachLimit').val();
	    	var startTime = $('#startTime').val();
	    	var endTime = $('#endTime').val();
	    	var companyId = $('#companyId').val();
	    	var discount = $('#discount').val();
	    	var money = $('#money').val();
				
			if(!title.trim()){
     			$.scojs_message("请输入简介", $.scojs_message.TYPE_ERROR);
     			 $("#submit_button").attr("disabled",false);
     			return false;
     		}

			if(couponType === "nonoe"){
				$.scojs_message("请选择服务类型", $.scojs_message.TYPE_ERROR);
				return false;
			}
   
	     	if($("#end").val()!=""){
				if($("#start").val()>$("#end").val()){
					 $.scojs_message("起始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
					 return false;
				}
			}
			
			if($("input[name='type']:checked").length==0){
			   $.scojs_message("请选择优惠券类型", $.scojs_message.TYPE_ERROR);
		 		return false;
			}
	 	
	     	var beyond=/^((0\.[1-9]?)|([1-9](\.\d)?))$/;
	     	var ismomey =/^[1-9]\d*$/;
			if($("input[name=type]:checked").val()==0){
				if($("#discount").val()==""){
					 $.scojs_message("请输入折扣", $.scojs_message.TYPE_ERROR);
		 			return false;
				}
				if(!beyond.test($("#discount").val())){
					 $.scojs_message("请输入正确的折扣（如：8.5或9）", $.scojs_message.TYPE_ERROR);
		 			return false;
				}
				
			}
			else if($("input[name=type]:checked").val()==1){
					if($("#money").val()==""){
						 $.scojs_message("请输入抵扣金额", $.scojs_message.TYPE_ERROR);
			 			return false;
					}
					if(!ismomey.test($("#money").val())){
						 $.scojs_message("请输入正确的抵扣（只能输入正整数，不能输入小数或0）", $.scojs_message.TYPE_ERROR);
			 			return false;
					}
					
				
			} 	
		    var couponTypeName = $('#couponTypeId').find("option:selected").text();
			$.post('${ctx}/couponrule/createCouponRule',
			{'title':title,'code':code,'type':type,'couponType':couponType,'couponTypeId':couponTypeId,
			'number':number,'eachLimit':eachLimit,'startTime':startTime,
			'couponTypeName':couponTypeName,
			'endTime':endTime,'companyId':companyId,'discount':discount,'money':money},
			function(data){
	           if(data.success){ 
						$('#submit_button').attr('disabled',false);
						window.location='${ctx}/couponrule/list';
		       }else{
	                $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
						$('#submit_button').attr('disabled',false);
		       }
		    });
			    
			return false;
		});
	
	});
</script>
</@override>
<@extends name="../layout.ftl"/>